/**
 * @author mapeng
 * @version 2017-05-09
 */

(function(doc, win) {

	var pageVariables = {"dispatchData" : {} , "dispatchPage" : ""};

	$(doc).on("pageInit", "#weixinSafeVerifyPage", function(e, pageId, $page, isBack) {
		init4Page();
	});
	bindEvents4Page();

	//页面初始化
	function init4Page(){

		var params = CommonUtil.takeOutParamObject() || {};
		pageVariables.dispatchData = params.dispatchData;
		pageVariables.dispatchPage = params.dispatchPage;
	}

	//绑定事件
	function bindEvents4Page(){

		//发送验证码
		$(document).on("click","#weixinSafeVerifyPage div.getCode",function(e){

			var $this = $(this);

			if($this.hasClass("disabled"))	return;

			CommonUtil.sendJsonpRequest({
			// weixinSafeVerifyTest.testSendVerifyCode({
				"url" : App.serverUrl+"app/wxAccount/sendVerifyCode4WCPay.do",
				"params" : {},
				"success" : function(result){
					if(result.success.toString() == "true"){//success
						DialogUtil.dialogWarn("发送成功");
						readSeconds4WeixinSafeVerifyPage($this,60);

					}else{//business error
						DialogUtil.dialogWarn(result.msg);
					}
				},
				"error" : function(){//not 200
					DialogUtil.dialogWarn("请求失败，请稍候重试");
				}
			});
		});

		//验证
		$(document).on("click","#weixinSafeVerifyPage .header a[name='verify']",function(e){
			// //将该次短信校验动作加入缓存
			// localStorageClient.save("sms","1",true);
						
			// //跳转
			// var cache = CommonUtil.takeOutParamObject() || {};
			// $.extend(cache,pageVariables.dispatchData,{"handleBack" : "load"});
			// CommonUtil.injectParamObject(cache);
			// $.router.load(pageVariables.dispatchPage,true);

			// return;

			var verifyCode = $("#weixinSafeVerifyPage input[name='verifyCode']").val();
			if(!/^[0-9]{6}$/.test(verifyCode)){
				DialogUtil.dialogWarn("请您输入六位数字验证码");
				return;
			}

			CommonUtil.sendJsonpRequest({
			// weixinSafeVerifyTest.testCheckVerifyCode({
				"url" : App.serverUrl+"/app/wxAccount/verifyCode4WCPay.do",
				"params" : {"verifyCode" : verifyCode},
				"success" : function(result){
					if(result.success.toString() == "true"){//success
						//将该次短信校验动作加入缓存
						var saveFlag = localStorageClient.save("sms","1",true);
						//跳转
						var cache = CommonUtil.takeOutParamObject() || {};
						$.extend(cache,pageVariables.dispatchData,{"handleBack" : "load"});
						CommonUtil.injectParamObject(cache);
						$.router.load(pageVariables.dispatchPage,true);
						//
						var sessionId = APP.sessionId;
					}else{//business error
						DialogUtil.dialogWarn(result.msg);
					}
				},
				"error" : function(){//not 200
					DialogUtil.dialogWarn("请求失败，请稍候重试");
				}
			});
		});
	}

	/**
	 * 读秒
	 * @param  {[jquery obj]} $obj    目标对象
	 * @param  {[int]} waitTime 秒数
	 */
	function readSeconds4WeixinSafeVerifyPage($obj,waitTime){
		var waitTime = waitTime ? waitTime : 60;
        var countdown = waitTime;
        settime($obj);
        function settime(val) {
            if (countdown == 0) {
                val.removeClass("disabled");
                val.text("重新发送");
                countdown = waitTime;
                clearTimeout();
            } else {
                val.addClass("disabled");
                val.text(countdown + "秒后重发");
                countdown--;
                setTimeout(function () {
                    settime(val);
                }, 1000)
            }
        }
	}

	/**
	 * 测试接口
	 * @type {Object}
	 */
	var weixinSafeVerifyTest = {
		"testSendVerifyCode" : function(options){//发送验证码
			options.success({
				"success" : true
			});
		},
		"testCheckVerifyCode" : function(options){//校验验证码
			options.success({
				"success" : true,
				"resultObject" : {
					
				}
				
			});
		}
	};

})(document, window);